package com.xyd.Servlet;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.xyd.Biz.CommodityBiz;
import com.xyd.Biz.Impl.CommodityBizImpl;
import com.xyd.Entity.Commodity;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/html/manage/SubmitAddProductServlet")
public class SubmitAddProductServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        SmartUpload sm = new SmartUpload();
        sm.initialize(javax.servlet.jsp.JspFactory.getDefaultFactory().getPageContext(this, request, response,
                null, true, 8192, true));
        sm.setCharset("UTF-8");

        try {
            sm.upload();
            Request smartReq = sm.getRequest();
            String pic =sm.getFiles().getFile(0).getFileName(); //smartReq.getParameter("pic");
            sm.save("html/images/product");



            String commodity_name = smartReq.getParameter("productName");
            String commod_id = smartReq.getParameter("parentId");
            String original_price = smartReq.getParameter("original_price");
            String now_price = smartReq.getParameter("now_price");
            String productcolor = smartReq.getParameter("productcolor");
            String productbrand = smartReq.getParameter("productbrand");
            String num = smartReq.getParameter("num");
            String content = smartReq.getParameter("context");

            CommodityBiz commodityBiz = new CommodityBizImpl();
            Commodity commodity = new Commodity();
            commodity.setCommodity_name(commodity_name);
            commodity.setContent(content);
            commodity.setNum(Integer.parseInt(num));
            commodity.setOriginal_price(Float.valueOf(original_price));
            commodity.setNow_price(Float.valueOf(now_price));
            commodity.setCommod_id(commod_id);
            commodity.setVal_type(1);
            commodity.setPic(pic);
            commodity.setProductcolor(productcolor);
            commodity.setProductbrand(productbrand);
            int result = commodityBiz.addProduct(commodity);
            if (result>0){
                request.getRequestDispatcher("manage-result.jsp").forward(request,response);
            }else {

            }


        } catch (SmartUploadException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
